#include "Schedule.h"

string to_lower_case(string in)
{
	char *c;
	c=new char[in.size()];
	strcpy(c,in.c_str());
	for (int i=0;i<in.size();i++)
	{
		c[i]=tolower(c[i]);
	}
	in=c;
	return in;
	delete []c;

}

Schedule::Schedule(string day, string start_time, string end_time, 
	string event_name, string place,LEVEL priority_level):
_day(day), _start_time(start_time), _end_time(end_time),
	_event_name(event_name), _place(place), _priority_level(priority_level)
{
}

void Schedule::set_day(string day)
{
	_day=day;
}

void Schedule::set_time(string start_time, string end_time)
{
	_start_time=start_time;
	_end_time=end_time;
}

void Schedule::set_place(string place)
{
	_place=place;
}

void Schedule::set_name(string name)
{
	_event_name=name;
}

void Schedule::set_priority(LEVEL priority)
{
	_priority_level=priority;
}

string Schedule::get_day()
{
	return _day;
}

string Schedule::get_start_time()
{
	return _start_time;
}

string Schedule::get_end_time()
{
	return _end_time;
}

string Schedule::get_name()
{
	return _event_name;
}

string Schedule::get_place()
{
	return _place;
}

Schedule::LEVEL Schedule::get_priority()
{
	return _priority_level;
}

bool Schedule::check_clash(Schedule* schedule)
{
	bool result; //return true if clash

	if(this->get_day()==schedule->get_day())
	{
		string start1=this->get_start_time();
		string end1=this->get_end_time();
		string start2=schedule->get_start_time();
		string end2=schedule->get_end_time();

		//1st event start before the 2nd event
		if(start1<start2)
		{
			//1st event end just before 2nd event start:
			if(end1<=start2)
			{
				result=false;
			}
			else //2nd event start before 1st event end
			{
				result=true;
			}
		}
		else if(start1>start2)//2nd event start first
		{
			//2nd event end just before 1st event start
			if(end2<=start1)
			{
				result=false; //no clash
			}
			else
			{
				result=true;
			}
		}
		else //if start1=start2
		{
			result=true;
		}
	}


	return result;
}

Schedule::LEVEL Schedule::priority_converter(string level)
{
	LEVEL result;
	if(level=="***")
	{
		result=HIGH;
	}
	else if(level=="**")
	{
		result=NORMAL;
	}
	else if(level=="*")
	{
		result=LOW;
	}
	return result;
}

string Schedule::priority_converter(LEVEL level)
{
	string result;
	switch(level)
	{
	case LOW: 
		result="*";
		break;
	case NORMAL:
		result="**";
		break;
	case HIGH:
		result="***";
		break;
	}
	return result;
}

string Schedule::get(DATATYPE type)
{
	string result;
	switch(type)
	{
	case DAY:
		result=_day;
		break;
	case NAME:
		result=_event_name;
		break;
	case STARTTIME:
		result=_start_time;
		break;
	case ENDTIME:
		result=_end_time;
		break;
	case PLACE:
		result=_place;
		break;
	case PRIORITY:
		result=Schedule::priority_converter(_priority_level);
		break;
	}
	return result;
}